package com.nikkerball.apps;

import com.nikkerball.apps.darkhorse.web.base.SecurePage;
import com.nikkerball.apps.darkhorse.web.security.SignInPage;
import com.nikkerball.apps.darkhorse.web.session.DarkhorseSession;
import org.apache.wicket.Application;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.authorization.strategies.page.SimplePageAuthorizationStrategy;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;

public class DarkHorseApplication extends WebApplication {

    // ----- constructors -----

    /**
     * Constructor
     */
    public DarkHorseApplication() {
        super();
    }

    // ----- public methods -----

    /**
     * @see Application#getHomePage()
     */
    public Class<HomePage> getHomePage() {
        return HomePage.class;
    }

    public void init() {
        super.init();
        addComponentInstantiationListener(new SpringComponentInjector(this));

        SimplePageAuthorizationStrategy authorizationStrategy = new SimplePageAuthorizationStrategy(SecurePage.class,
                SignInPage.class) {
            protected boolean isAuthorized() {
                return ((DarkhorseSession) Session.get()).isSignedIn();
            }
        };
        getSecuritySettings().setAuthorizationStrategy(authorizationStrategy);

    }

    @Override
    public Session newSession(Request request, Response response) {
        DarkhorseSession session = new DarkhorseSession((ServletWebRequest) request);
        return session;
    }

}
